#ifndef GAMEWINDOW_H
#define GAMEWINDOW_H

#include <QGLWidget>
#include <QVector>
#include <QVector2D>
#include <QVector3D>

#include <vector>
#include <cv.h>
#include <fmod.hpp>

class GameWindow : public QGLWidget
{
    Q_OBJECT
public:
	explicit GameWindow(QWidget *parent = 0, QGLWidget *share_widget = 0);
	~GameWindow();
	void renderContext(unsigned int elapsed_time);
	void setAngle(float angle);
	void setRunning(bool running);
	void setWindowSize(int width, int height);
	void setSidePanel(bool img, bool mask);
	void setCameraTextures(const cv::Mat& camera, const cv::Mat& mask);
	void restart();
	void addBullet();

protected:
	void initializeGL();
	void paintGL();
	void resizeGL(int width, int height);
	void keyPressEvent (QKeyEvent *event);

signals:
	void closeGame();
	void endGame();
	void emitScore(int score_);
	void emitPurpose(QString purpose);

private:
	struct Bird {
		float posX, posZ;
		int type;
	};
	struct Bullet {
		float posX, posZ;
	};

	void createTexture();
	float computeNewX(float x, float z);
	float planeAngle_;
	float planePos_;
	int frameWidth_;
	int frameHeight_;
	GLuint backgroundTexture_;
	GLuint planeTexture_;
	GLuint cameraTexture_;
	GLuint maskTexture_;
	GLuint hpTexture_;
	GLuint birdsTexture_[2];
	GLuint bulletTexture_;
	GLuint cityTexture_;
	GLuint scoreTexture_;
	GLuint bulletIconTexture_;
	float hpValue_;
	float cityValue_;
	QVector<QVector3D> vertices_;
	QVector<QVector2D> texCoords_;
	QSize planeSize_;
	bool isRunning_;
	int windowWidth_;
	int windowHeight_;
	bool isImgPanel_;
	bool isMaskPanel_;
	std::vector<Bird> birds_;
	std::vector<Bullet> bullets_;
	int bulletsNum_;
	int nextShot_;
	int nextRestore_;
	bool bulletAdded_;

	static int shotTime_;
	static int bulletsRestoreTime_;
	static int maxBullets_;

	static float planeWidth_;
	static float planeHeight_;
	static float birdsVelocity_;
	static float birdsAcceleration_;
	static float planeVelocity_;
	static float bulletVelocity_;
	int nextBird_;
	int score_;
	void collisionProcess();
	void drawBulletsNum(bool isPanel);
	void drawScore(bool isPanel);
	float transformPos(float val, bool isPanel);
	float transformWidth(float val, bool isPanel);

	//music
	FMOD::System *fmodSystem_;
	FMOD::Sound* sounds_[4];
	FMOD::Channel *planeChannel_, *otherChannel_;
};

#endif // GAMEWINDOW_H
